/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import marsaMaroc.dao.ClientDAO;
import marsaMaroc.dao.LoginDAO;
import marsaMaroc.model.Client;
import marsaMaroc.model.Compte;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Administrateur
 */
@Controller
public class LoginController {

    LoginDAO loginDAO = new LoginDAO();

    @RequestMapping(value = "/Login.htm", method = RequestMethod.POST)
    protected String add(
            @RequestParam("nomUser") String nomUser,
            @RequestParam("passwordUser") String passwordUser, HttpServletRequest request) throws Exception {

        HttpSession session = request.getSession();
        Compte c = loginDAO.login(nomUser, passwordUser);
       
        if (c != null) {
            if (c.getRoles().getRoles().equals("Admin")) {
                session.setAttribute("compte", c);
                return "redirect:/admin.htm";
            } else if (c.getRoles().getRoles().equals("Client")) {
                session.setAttribute("compte", c);
                Client cl = new ClientDAO().getByAccount(c.getId());
                session.setAttribute("client", cl);
                return "redirect:/dashboard.htm";
            } else{
                return "index";
            }
        } else {
           
            session.setAttribute("msg", "non trouvé");
       
            return "redirect:/Acceuil.htm";
        }  
    }
}
